/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.letmeshare.stockkeeper.view.dashboard;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.table.DefaultTableModel;
import org.letmeshare.stockkeeper.component.CateTableModel;
import org.letmeshare.stockkeeper.component.StockTableModel;
import org.letmeshare.stockkeeper.stock.entities.Category;
import org.letmeshare.stockkeeper.stock.entities.Item;
import org.letmeshare.stockkeeper.stock.entities.Stock;
import org.letmeshare.stockkeeper.stock.service.StockService;

/**
 *
 * @author abin
 */
public class StoreRoom extends javax.swing.JPanel {

    private StockService stockService;
    private Stock selectedStock;

    /**
     * Creates new form StoreRoom
     */
    public StoreRoom() {
        stockService = StockService.getInstance();
        initComponents();
        registerHandlers();
        initScreen();
        clearComponents();
        jCheckBox1.setVisible(false);
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        l_catName = new javax.swing.JLabel();
        l_catCode = new javax.swing.JLabel();
        catName = new javax.swing.JTextField();
        catCode = new javax.swing.JTextField();
        addNewitem = new javax.swing.JButton();
        jPanel2 = new javax.swing.JPanel();
        jPanel3 = new javax.swing.JPanel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        jCheckBox1 = new javax.swing.JCheckBox();
        itemCode = new javax.swing.JTextField();
        itemName = new javax.swing.JTextField();
        itemPrice = new javax.swing.JTextField();
        itemTax = new javax.swing.JTextField();
        addItem = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        stockTable = new javax.swing.JTable();
        jLabel8 = new javax.swing.JLabel();

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(""));

        jLabel1.setText("Create New Category");

        l_catName.setText("Cat : Name");

        l_catCode.setText("Cat : Code");

        catName.setText("");


        catCode.setText("");

        addNewitem.setText(" Add New Item");


        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
                jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup().addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING).addGroup(jPanel1Layout.createSequentialGroup().addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE).addComponent(addNewitem)).addGroup(jPanel1Layout.createSequentialGroup().addGap(18, 18, 18).addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING).addComponent(l_catName, javax.swing.GroupLayout.Alignment.LEADING).addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 144, javax.swing.GroupLayout.PREFERRED_SIZE)).addComponent(l_catCode)).addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE).addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false).addComponent(catName, javax.swing.GroupLayout.DEFAULT_SIZE, 143, Short.MAX_VALUE).addComponent(catCode)))).addGap(74, 74, 74)));
        jPanel1Layout.setVerticalGroup(
                jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGroup(jPanel1Layout.createSequentialGroup().addContainerGap().addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 22, javax.swing.GroupLayout.PREFERRED_SIZE).addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED).addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE).addComponent(l_catName).addComponent(catName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)).addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED).addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE).addComponent(l_catCode).addComponent(catCode, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)).addGap(18, 18, 18).addComponent(addNewitem).addContainerGap(29, Short.MAX_VALUE)));

        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder("Item"));

        jLabel4.setText("Name ");

        jLabel5.setText("Code");

        jLabel6.setText("Price");

        jLabel7.setText("Ex.Tax");

        jCheckBox1.setText("Is Obsolete");


        itemCode.setText("item code");

        itemName.setText("Item name");

        itemPrice.setText("prie");

        itemTax.setText("tax");

        addItem.setText("Add item");

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
                jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGroup(jPanel3Layout.createSequentialGroup().addContainerGap().addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false).addComponent(jCheckBox1).addGroup(jPanel3Layout.createSequentialGroup().addComponent(jLabel4).addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED).addComponent(itemName)).addGroup(jPanel3Layout.createSequentialGroup().addComponent(jLabel5).addGap(18, 18, 18).addComponent(itemCode, javax.swing.GroupLayout.PREFERRED_SIZE, 162, javax.swing.GroupLayout.PREFERRED_SIZE)).addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false).addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel3Layout.createSequentialGroup().addComponent(jLabel7).addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED).addComponent(itemTax)).addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel3Layout.createSequentialGroup().addComponent(jLabel6).addGap(18, 18, 18).addComponent(itemPrice, javax.swing.GroupLayout.PREFERRED_SIZE, 74, javax.swing.GroupLayout.PREFERRED_SIZE)))).addContainerGap(107, Short.MAX_VALUE)).addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel3Layout.createSequentialGroup().addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE).addComponent(addItem).addGap(58, 58, 58)));
        jPanel3Layout.setVerticalGroup(
                jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGroup(jPanel3Layout.createSequentialGroup().addGap(22, 22, 22).addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE).addComponent(jLabel4).addComponent(itemName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)).addGap(18, 18, 18).addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE).addComponent(jLabel5).addComponent(itemCode, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)).addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED).addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE).addComponent(jLabel6).addComponent(itemPrice, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)).addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED).addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE).addComponent(jLabel7).addComponent(itemTax, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)).addGap(18, 18, 18).addComponent(jCheckBox1).addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 26, Short.MAX_VALUE).addComponent(addItem).addGap(65, 65, 65)));

        stockTable.setModel(new javax.swing.table.DefaultTableModel(
                new Object[][]{
                    {null, null, null, null},
                    {null, null, null, null},
                    {null, null, null, null},
                    {null, null, null, null}
                },
                new String[]{
                    "Title 1", "Title 2", "Title 3", "Title 4"
                }));
        jScrollPane1.setViewportView(stockTable);

        jLabel8.setText("Select Item To edit");

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
                jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGroup(jPanel2Layout.createSequentialGroup().addGap(2, 2, 2).addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE).addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGroup(jPanel2Layout.createSequentialGroup().addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED).addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)).addGroup(jPanel2Layout.createSequentialGroup().addGap(21, 21, 21).addComponent(jLabel8, javax.swing.GroupLayout.PREFERRED_SIZE, 92, javax.swing.GroupLayout.PREFERRED_SIZE))).addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)));
        jPanel2Layout.setVerticalGroup(
                jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGroup(jPanel2Layout.createSequentialGroup().addContainerGap().addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGroup(jPanel2Layout.createSequentialGroup().addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE).addGap(0, 0, Short.MAX_VALUE)).addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup().addComponent(jLabel8, javax.swing.GroupLayout.DEFAULT_SIZE, 32, Short.MAX_VALUE).addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED).addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 312, javax.swing.GroupLayout.PREFERRED_SIZE))).addContainerGap()));

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGroup(layout.createSequentialGroup().addContainerGap().addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE).addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)).addContainerGap(22, Short.MAX_VALUE)));
        layout.setVerticalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGroup(layout.createSequentialGroup().addGap(21, 21, 21).addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE).addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED).addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE).addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)));
    }// </editor-fold>

    private void registerHandlers() {
       
        addItem.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                if(e.getActionCommand().toLowerCase().equals("add item")){
                Item item = new Item();
                item.setName(itemName.getText());
                item.setCode(itemCode.getText());
                item.setUnitPrice(Double.valueOf(itemPrice.getText().isEmpty() ? "0" : itemPrice.getText()));
                item.setExclusiveTax(Double.valueOf(itemTax.getText().isEmpty() ? "0" : itemTax.getText()));
                item.setObsolete(jCheckBox1.isSelected());
                    try {
                        stockService.createItem(item);
                        clearComponents();
                        initScreen();
                    } catch (Exception exception) {
                         JOptionPane.showMessageDialog(addItem, exception.getMessage(), "Warning Message",JOptionPane.ERROR_MESSAGE);
                    }
                    
            }else if(e.getActionCommand().toLowerCase().equals("save")){
                if(selectedStock!=null){
                selectedStock.getItem().setName(itemName.getText());
                 selectedStock.getItem().setCode(itemCode.getText());
                 selectedStock.getItem().setUnitPrice(Double.valueOf(itemPrice.getText().isEmpty() ? "0" : itemPrice.getText()));
                 selectedStock.getItem().setExclusiveTax(Double.valueOf(itemTax.getText().isEmpty() ? "0" : itemTax.getText()));
                 selectedStock.getItem().setObsolete(jCheckBox1.isSelected());
                 try{
                     stockService.updateItem(selectedStock.getItem());
                     addItem.setText("Add item");
                     selectedStock=null;
                      itemCode.setEditable(true);
                     initScreen();
                     clearComponents();
                 }catch(Exception exception){
                      JOptionPane.showMessageDialog(addItem, exception.getMessage(), "Warning Message",JOptionPane.ERROR_MESSAGE);
                 }
            }
            }
            }
        });
        addNewitem.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                selectedStock=null;
                initScreen();
                itemCode.setEditable(true);
                 addItem.setText("Add item");
            }
        });
        stockTable.addMouseListener(new MouseListener() {

            public void mouseClicked(MouseEvent e) {
                // throw new UnsupportedOperationException("Not supported yet.");

                try {
                    String stockId = "" + stockTable.getModel().getValueAt(stockTable.getSelectedRow(), 0);
                    StockService stockService = StockService.getInstance();
                    selectedStock = stockService.getStock(stockId);
                    onItemSelect();
                }  catch (Exception exception) {
                   // JOptionPane.showMessageDialog(stockTable, exception.getMessage(), "Warning Message",JOptionPane.ERROR_MESSAGE);
                }
            }

            public void mousePressed(MouseEvent e) {
                // throw new UnsupportedOperationException("Not supported yet.");
            }

            public void mouseReleased(MouseEvent e) {
                // throw new UnsupportedOperationException("Not supported yet.");
            }

            public void mouseEntered(MouseEvent e) {
                // throw new UnsupportedOperationException("Not supported yet.");
            }

            public void mouseExited(MouseEvent e) {
                // throw new UnsupportedOperationException("Not supported yet.");
            }
        });
    }

    private void clearComponents() {
        itemCode.setText("");
        itemName.setText("");
        itemPrice.setText("");
        itemTax.setText("");
        catCode.setVisible(false);
        catName.setVisible(false);
        l_catCode.setVisible(false);
        l_catName.setVisible(false);
        jLabel1.setVisible(false);
    }

    private void initScreen() {
        stockTable.setModel(new StockTableModel(stockService.getAllStocks()));
        stockTable.repaint();jCheckBox1.setVisible(false);
    }

    private void onItemSelect() {
        if (selectedStock != null) {
            itemCode.setText(selectedStock.getItem().getCode());
            itemName.setText(selectedStock.getItem().getName());
            itemPrice.setText("" + selectedStock.getItem().getUnitPrice());
            itemTax.setText("" + selectedStock.getItem().getExclusiveTax());
            addItem.setText("Save");
            itemCode.setEditable(false);
            jCheckBox1.setVisible(true);
        }
    }
    // Variables declaration - do not modify
    private javax.swing.JButton addNewitem;
    private javax.swing.JButton addItem;
    private javax.swing.JCheckBox jCheckBox1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel l_catName;
    private javax.swing.JLabel l_catCode;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable stockTable;
    private javax.swing.JTextField catName;
    private javax.swing.JTextField itemCode;
    private javax.swing.JTextField catCode;
    private javax.swing.JTextField itemName;
    private javax.swing.JTextField itemPrice;
    private javax.swing.JTextField itemTax;
    // End of variables declaration
}
